<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Assets Module Helper
 *
 * $Id: assets.php 111 2008-09-02 17:13:24Z alex.aperez $
 *
 * @package		Assets Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class assets_Core 
{
	/**
	 * stylesheet
	 * Creates a stylesheet link.
	 *
	 * @param string $style filename
	 * @param string $media media type of stylesheet
	 * @param boolean $index include the index_page in the link
	 * @return string An HTML stylesheet link.
	 * @access public
	 * 
	 */
	public static function stylesheet($style, $media = FALSE, $index = TRUE)
	{
		return html::link(self::glue($style, '.css'), 'stylesheet', 'text/css', '.css', $media, $index);
	}

	/**
	 * script
	 * Creates a script link.
	 *
	 * @param string $script filename
	 * @param string $index include the index_page in the link
	 * @return string An HTML script link.
	 * @access public
	 * 
	 */
	public static function script($script, $index = TRUE)
	{
		$script = rtrim(self::glue($script, '.js'), '.js').'.js';
		return '<script type="text/javascript" src="'.url::base((bool) $index).$script.'"></script>';
	}

	/**
	 * glue
	 * Takes an array of files and creates a new one containing all contents.
	 * Returns the name of created file
	 * 
	 * @param array $files
	 * @param string $ext
	 * @return string
	 * @access private
	 * 
	 */
	private static function glue($files, $ext)
	{
		$local_path = APPPATH.'views/';
	
		$files = array_unique( (array) $files);

		$filename = 'assets/'.str_replace('.', '', $ext).'/'.md5(implode('', $files));
		$filesrc = $local_path.$filename.$ext;

		if ( ! file_exists($filesrc) or filemtime($filesrc) < self::get_last_modified($files, $local_path, $ext.EXT)) 
		{
			ob_start();
				
			foreach($files as $script) 
			{
				$suffix = (strpos($script, $ext) === FALSE) ? $ext : '';
				echo View::factory($script.$suffix);
			}
				
			file_put_contents($filesrc, ob_get_clean(), LOCK_EX);
		}
		
		return $filename;
	}

	/**
	 * get_last_modified
	 * Takes an array of filenames and returns the most recent modified date
	 * 
	 * @param array $files
	 * @param string $path
	 * @param string $ext
	 * @return int
	 * @access private
	 * 
	 */
	private static function get_last_modified($files, $path, $ext)
	{
		$last_modified = 0;
		
		foreach($files as $file) 
		{
			$suffix = (strpos($file, $ext) === FALSE) ? $ext : '';
			$modified = filemtime($path.$file.$suffix);
			
			if($modified !== false and $modified > $last_modified) 
				$last_modified = $modified;
		}

		return $last_modified;
	}

} // End Assets Helper